import { createRouter, createWebHashHistory } from 'vue-router';
import Layout from '@/components/layout.vue';

const routes = [
  { path: '/', redirect: '/main' },
  { path: '/login', name: 'login', component: () => import("@/view/login.vue") },
  { path: '/main', name: 'main', component: Layout, redirect: '/home', children: [
      { path: '/home', name: 'home', component: () => import("@/view/home/index.vue")},
    ]
  },
  { path: '/goods', name: '商品管理', component: Layout, children: [
      { path: 'goods-list', name: '商品列表', component: () => import("@/view/goods/index.vue")},
      { path: 'goods-info', name: '商品详情', component: () => import("@/view/goodsInfo/index.vue")},
    ]
  },
  { path: '/user', name: "用户管理", component: Layout, children: [
    { path: 'user-list', name: '用户列表', component: () => import("@/view/user/index.vue") },
    { path: 'user-setting', name: '用户设置', component: () => import("@/view/user/userSetting.vue") },
    ]
  },
  { path: "/:pathMathc(.*)*", name: "NotFound", component: () => import("@/view/notFound/index.vue") }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
router.beforeEach((to, from , next) => {
  if (to.path === '/login') return next();

  const token = sessionStorage.getItem("token");
  if (!token) return next("/login");
  next();
});

export default router;